﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace QuanLyKho.GUI.Library
{
    public class clsPaging
    {
        public string RecordID1 = "";
        public string RecordID2 = "";
        static public int CurrentPage = 1;
        static public int totalPage = 0;

        public void fillDataGrid(DataSet dsData, BindingSource bindingSource1, DataGridView dgv, string NavClicked, Label lblPaging, int TotalRows)
        {
            try
            {
                bindingSource1.DataSource = dsData.Tables[0];
                DeterminePageBoundaries(dsData, NavClicked, TotalRows);
                bindingSource1.Filter = "STT >= " + RecordID1 + " and STT <= " + RecordID2;
                dgv.DataSource = bindingSource1;

                lblPaging.Text = "Trang " + CurrentPage + "/" + totalPage;
            }
            catch { }
        }

        public void DeterminePageBoundaries(DataSet dsPages, string NavClicked, int TotalRows)
        {
            int TotalRowCount = dsPages.Tables[0].Rows.Count;
            int pageRows = TotalRows;
            int pages = 0;
            if (pageRows < TotalRowCount)
            {
                if ((TotalRowCount % pageRows) > 0)
                    pages = ((TotalRowCount / pageRows) + 1);
                else
                    pages = TotalRowCount / pageRows;
            }
            else
            {
                pages = 1;
            }

            totalPage = pages;

            int LoweBoundary = 0;
            int UpperBoundary = 0;

            switch (NavClicked)
            {
                case "First":
                    CurrentPage = 1;
                    LoweBoundary = ((pageRows * CurrentPage) - (pageRows - 1));
                    if (pageRows < TotalRowCount)
                        UpperBoundary = (pageRows * CurrentPage);
                    else
                        UpperBoundary = TotalRowCount;

                    RecordID1 = dsPages.Tables[0].Rows[LoweBoundary - 1]["STT"].ToString();
                    RecordID2 = dsPages.Tables[0].Rows[UpperBoundary - 1]["STT"].ToString();
                    break;

                case "Last":
                    CurrentPage = pages;
                    LoweBoundary = ((pageRows * CurrentPage) - (pageRows - 1));
                    UpperBoundary = TotalRowCount;

                    RecordID1 = dsPages.Tables[0].Rows[LoweBoundary - 1]["STT"].ToString();
                    RecordID2 = dsPages.Tables[0].Rows[UpperBoundary - 1]["STT"].ToString();
                    break;

                case "Next":
                    if (CurrentPage != pages)
                        CurrentPage += 1;

                    LoweBoundary = ((pageRows * CurrentPage) - (pageRows - 1));

                    if (CurrentPage == pages)
                        UpperBoundary = TotalRowCount;
                    else
                        UpperBoundary = (pageRows * CurrentPage);

                    RecordID1 = dsPages.Tables[0].Rows[LoweBoundary - 1]["STT"].ToString();
                    RecordID2 = dsPages.Tables[0].Rows[UpperBoundary - 1]["STT"].ToString();
                    break;

                case "Previous":
                    if (CurrentPage != 1)
                        CurrentPage -= 1;
                    LoweBoundary = ((pageRows * CurrentPage) - (pageRows - 1));
                    if (pageRows < TotalRowCount)
                        UpperBoundary = (pageRows * CurrentPage);
                    else
                        UpperBoundary = TotalRowCount;

                    RecordID1 = dsPages.Tables[0].Rows[LoweBoundary - 1]["STT"].ToString();
                    RecordID2 = dsPages.Tables[0].Rows[UpperBoundary - 1]["STT"].ToString();
                    break;

                default:
                    LoweBoundary = ((pageRows * CurrentPage) - (pageRows - 1));
                    if (pageRows < TotalRowCount)
                        UpperBoundary = (pageRows * CurrentPage);
                    else
                        UpperBoundary = TotalRowCount;

                    RecordID1 = dsPages.Tables[0].Rows[LoweBoundary - 1]["STT"].ToString();
                    RecordID2 = dsPages.Tables[0].Rows[UpperBoundary - 1]["STT"].ToString();
                    break;
            }
        }
    }
}
